<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>神奇字符串</title>
</head>

<body>
  <script>
    var magicalString = function (n) {
      const s = [1, 2, 2];
      for (let i = 2; s.length < n; i++) {
        const last = s[s.length - 1];
        for (let j = 0; j < s[i]; j++) {
          s.push(last ^ 3);
        }
      }
      return s.slice(0, n).filter(x => x === 1).length;
    };
    console.log(magicalString(6));
  </script>
</body>

</html>